package com.neuronbit.businessflow.runtime;

import com.neuronbit.businessflow.runtime.exec.BusinessFlow;
import com.neuronbit.businessflow.runtime.lifecycle.FlowLifecycleHook;
import com.neuronbit.businessflow.runtime.lifecycle.StepLifecycleHook;
import com.neuronbit.businessflow.runtime.repo.BusinessFlowRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = {"com.neuronbit.businessflow"})
public class BusinessFlowAutoConfiguration {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public BusinessFlowAutoConfiguration() {
        log.info("BusinessFlowAutoConfiguration initialized");
    }

    @Bean
    public BusinessFlowRepository businessFlowRepository(DataSource dataSource) {
        return new JdbcBusinessFlowRepository(dataSource);
    }

    @Bean
    public StepLifecycleHook bfStepLifecycleHook(BusinessFlowRepository repository) {
        return new StepLifecycleHookImpl(repository);
    }

    @Bean
    public FlowLifecycleHook bfFlowLifecycleHook(BusinessFlowRepository repository) {
        return new FlowLifecycleHookImpl(repository);
    }

    @Bean
    public StepBeanProvider bfStepBeanProvider(ApplicationContext context) {
        return new SpringStepBeanProvider(context);
    }

    @Bean
    public BusinessFlow businessFlow(BusinessFlowRepository repository, StepBeanProvider stepBeanProvider) {
        return new BusinessFlow(repository, stepBeanProvider);
    }
}
